package com.web.cms.doc.dao;

import com.blue.jdbc.BaseDao;
import com.web.cms.doc.model.Doc;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("cmsDocDao")
public class DocDao extends BaseDao<Doc>
{
	public DocDao()
	{
	}

	@Override
	protected void query(StringBuilder sql, Doc param)
	{
		if (param != null)
		{
			if (param.getState() != null)
				sql.append(" and a.state=:state");
			
			if (param.getChannelId() != null && param.getChannelId() > 0)
				sql.append(" and a.channel_id=:channelId");
			
			if (param.getWebId() != null && param.getWebId() > 0)
				sql.append(" and a.web_id=:webId");
			
			if (param.getTitle() != null && !param.getTitle().isEmpty())
			{
				sql.append(" and a.title like :title");
				param.setTitle("%" + param.getTitle() + "%");
			}
			
		}
		sql.append(" order by a.sort desc, a.id desc");
	}
	
	public String getContentUrl(Integer id)
	{
		String sql = "select content_url from cms_doc where id=?";
		List<String> list = jdbcTemplate.queryForList(sql, String.class, id);
		if (list == null || list.isEmpty())
			return null;
		
		return list.get(0);
	}
	
	
}
